import 'package:flutter/material.dart';
import 'package:flutter_cate/core/extension/int_extension.dart';
import 'package:flutter_cate/core/model/meal_model.dart';

class AMDetailContent extends StatelessWidget {
  final AMMealModel _meal;
  AMDetailContent(this._meal);

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          buildBannerImage(),
          buildTitle(context, "制作材料"),
          buildMaterial(context),
          buildTitle(context, "制作步骤"),
          buildSteps(context),
        ],
      ),
    );
  }

  // 1. 横幅图片
  Widget buildBannerImage() {
    return Container(
      width: double.infinity,
      child: Image.network(_meal.imageUrl),
    );
  }

  // 2. 制作材料
  Widget buildMaterial(BuildContext context) {
    return buildContent(
        context: context,
        child: ListView.builder(
          padding: EdgeInsets.zero,
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          itemCount: _meal.ingredients.length,
          itemBuilder: (ctx, index) {
            return Card(
              color: Theme.of(ctx).accentColor,
              child: Padding(
                padding:
                    const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
                child: Text(_meal.ingredients[index]),
              ),
            );
          },
        ));
  }

  // 3.制作步骤
  Widget buildSteps(BuildContext context) {
    return buildContent(
      context: context,
      child: ListView.separated(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          padding: EdgeInsets.zero,
          itemBuilder: (ctx, index) {
            return ListTile(
              leading: CircleAvatar(
                child: Text("#${index + 1}"),
              ),
              title: Text(_meal.steps[index]),
            );
          },
          separatorBuilder: (ctx, index) {
            return Divider();
          },
          itemCount: _meal.steps.length),
    );
  }

  // 公共方法
  Widget buildTitle(BuildContext ctx, String title) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 12.px),
      child: Text(
        title,
        style: Theme.of(ctx)
            .textTheme
            .headline3
            .copyWith(fontWeight: FontWeight.bold),
      ),
    );
  }

  Widget buildContent({BuildContext context, Widget child}) {
    return Container(
      padding: EdgeInsets.all(8.px),
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(color: Colors.grey),
          borderRadius: BorderRadius.circular(8.px)),
      width: MediaQuery.of(context).size.width - 30.px,
      child: child,
    );
  }
}
